package com.bike.controller.api;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bike.controller.BaseController;
import com.bike.entity.HhBusinessUser;
import com.bike.entity.HhFood;
import com.bike.entity.HhFoodMenu;
import com.bike.service.impl.HhFoodMenuServiceImpl;
import com.bike.service.impl.HhFoodServiceImpl;
import com.bike.util.Constant;
import com.bike.util.PageData;
import com.bike.vo.PcJsonView;
import com.fasterxml.jackson.annotation.JsonView;

 
@Controller
@RequestMapping(value="/apiFood")
public class HhApiFoodController extends BaseController {
	
		@Autowired
		private HhFoodMenuServiceImpl  hhFoodMenuServiceImpl;
		@Autowired
		private HhFoodServiceImpl  hhFoodServiceImpl;
	
		 
		/**
		 * 商品菜单列表 查询(或根据条件)
		 */
		@RequestMapping("menuList")
		@ResponseBody
		@JsonView(PcJsonView.class)
		public PageData menuList(HttpServletRequest request,Model model){
			PageData pd=PageData.getPageDataByPage(request);
			
			HhBusinessUser businessUser=(HhBusinessUser) request.getAttribute("businessUser");
			pd.put("businessId", businessUser.getBusinessId());
			PageData rows=  hhFoodMenuServiceImpl.selectHhFoodMenuList(pd);
			PageData result=getSuccessPd(rows);
			return result;
		}
		/**
		 * 商品菜单新增 
		 * param  商品菜单名称  name    排序sorts
		 */
		@RequestMapping("saveMenu")
		@ResponseBody
		public PageData saveMenu(HttpServletRequest request,HhFoodMenu foodMenu,Model model){
			PageData pd=PageData.getToPageData(request);
			HhBusinessUser businessUser=(HhBusinessUser) request.getAttribute("businessUser");
			logger.debug("foodMenuId========"+foodMenu.getFoodMenuId());
			
			long foodMenuId=pd.getLong("foodMenuId");
			if(foodMenuId>0){
				HhFoodMenu foodMenu2=hhFoodMenuServiceImpl.selectByHhFoodMenuId(foodMenuId);
//				if(!foodMenu2.getBusinessId().equals(businessUser.getBusinessId())){
//						return getErrorPd("操作失败,请重试");
//				}
			}
			foodMenu.setBusinessId(businessUser.getBusinessId());
			int result= hhFoodMenuServiceImpl.saveOrUpdate(pd, foodMenu);
			
			if(result>0){
				return getSuccessPd();
			}
			return getErrorPd("系统异常,请重试");
		}
		/**
		 * 删除菜单
		 * @param request
		 * @param foodMenu
		 * @param model
		 * @return
		 */
		@RequestMapping("deleteMenu")
		@ResponseBody
		public PageData deleteMenu(HttpServletRequest request,HhFoodMenu foodMenu,Model model){
			PageData pd=PageData.getToPageData(request);
			HhBusinessUser businessUser=(HhBusinessUser) request.getAttribute("businessUser");
			
			HhFoodMenu menu=hhFoodMenuServiceImpl.selectByHhFoodMenuId( foodMenu.getFoodMenuId());
//			if(menu.getBusinessId().longValue()==businessUser.getBusinessId()){
				HhFoodMenu deleteMenu=new HhFoodMenu();
				deleteMenu.setFoodMenuId(menu.getFoodMenuId());
				deleteMenu.setStatus(Constant.delete);
				int result= hhFoodMenuServiceImpl.saveOrUpdate(pd, deleteMenu);
				if(result>0){
					return getSuccessPd();
				}
//			}
			return getErrorPd("系统异常,请重试");
		}
		
		/**
		 * 商品列表 查询(或根据条件)
		 * param foodMenuId
		 */
		@RequestMapping("list")
		@ResponseBody
		@JsonView(PcJsonView.class)
		public PageData list(HttpServletRequest request,Model model){
			PageData pd=PageData.getPageDataByPage(request);
			
			HhBusinessUser businessUser=(HhBusinessUser) request.getAttribute("businessUser");
			pd.put("businessId", businessUser.getBusinessId());
			PageData rows=  hhFoodServiceImpl.selectHhFoodList(pd);
			PageData result=getSuccessPd(rows);
			return result;
		}
		
		/**
		 * 商品新增(或根据条件)
		 * param  商品菜单Id foodMenuId
		 *  name  名称
		 * 	sorts 排序
		 * 	isMulttag 1是 0否
		 * 	price 元
		 * 	foodDesc 描述
		 */
		@RequestMapping("saveFood")
		@ResponseBody
		public PageData saveFood(HttpServletRequest request,HhFood food,Model model){
			PageData pd=PageData.getToPageData(request);
			
			HhBusinessUser businessUser=(HhBusinessUser) request.getAttribute("businessUser");
			HhFoodMenu foodMenu=hhFoodMenuServiceImpl.selectByHhFoodMenuId(pd.getLong("foodMenuId")); 
			if(foodMenu.getBusinessId().longValue()==businessUser.getBusinessId()){
				int result= hhFoodServiceImpl.saveOrUpdate(pd, food);
				if(result>0){
					return getSuccessPd();
				}
			}
			return getErrorPd("系统异常,请重试");
			
		}
		/**
		 * 商品售罄
		 * param  foodId
		 */
		@RequestMapping("updateFood")
		@ResponseBody
		public PageData updateFood(HttpServletRequest request,HhFood food,Model model){
			PageData pd=PageData.getToPageData(request);
			 
			HhFood foodRecord=hhFoodServiceImpl.selectByHhFoodId(food.getFoodId());
			HhBusinessUser businessUser=(HhBusinessUser) request.getAttribute("businessUser");
			if(foodRecord!=null&&foodRecord.getBusinessId().longValue()==businessUser.getBusinessId()){
				if(foodRecord.getDisabled()==0){
					food.setDisabled(1);//售罄
				}else{
					food.setDisabled(0);
				}
				int result= hhFoodServiceImpl.saveOrUpdate(pd, food);
				if(result>0){
					return getSuccessPd();
				}
			}
			return getErrorPd("系统异常,请重试");
		}
		/**
		 * 删除菜品
		 * @param request
		 * @param food
		 * @param model
		 * @return
		 */
		@RequestMapping("deleteFood")
		@ResponseBody
		public PageData deleteFood(HttpServletRequest request,HhFood food,Model model){
			PageData pd=PageData.getToPageData(request);
			
			HhFood foodRecord=hhFoodServiceImpl.selectByHhFoodId(food.getFoodId());
			HhBusinessUser businessUser=(HhBusinessUser) request.getAttribute("businessUser");
			if(foodRecord!=null&&foodRecord.getBusinessId().longValue()==businessUser.getBusinessId()){
				HhFood deleteFood=new HhFood();
				deleteFood.setFoodId(foodRecord.getFoodId());
				deleteFood.setStatus(Constant.delete);
				int result= hhFoodServiceImpl.saveOrUpdate(pd, deleteFood);
				if(result>0){
					return getSuccessPd();
				}
			}
			return getErrorPd("系统异常,请重试");
		}
	 
}
